package com.sqbr.video.controller;


import cn.dev33.satoken.stp.SaTokenInfo;
import com.sqbr.video.core.response.R;
import com.sqbr.video.core.utlis.IdUtil;
import com.sqbr.video.domain.form.UserPwdLoginForm;
import com.sqbr.video.domain.form.UserRegisterForm;
import com.sqbr.video.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-11-06
 */
@RestController
@Api(value = "用户接口")

@RequestMapping("/user")
@RequiredArgsConstructor
public class UserController {

    private final IUserService userService;


    @ApiOperation(
            value = "用户注册功能",
            notes ="该接口提供注册功能，实现幂等性注册逻辑，可高并发注册",
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE
    )
    @PostMapping(value = "/register")
    public R<String> register(@Validated @RequestBody UserRegisterForm userRegisterForm) {
        return R.data(userService.register(userRegisterForm));
    }

    @ApiOperation(value = "用户密码登录接口",
            notes = "该接口提供了用户密码登录的功能，登录成功后返回有效时accessToken供后续服务",
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @PostMapping(value = "/pwdLogin")
    public R<SaTokenInfo> pwdLogin(@Validated @RequestBody UserPwdLoginForm userPwdLoginForm) {
        return R.data(userService.pwdLogin(userPwdLoginForm));
    }

    @ApiOperation(value = "用户登出接口", notes = "该接口提供了用户登出的功能")
    @PostMapping("/exit")
    public R exit() {
        userService.exit();
        return R.success();
    }

}
